package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.common.utils.text.Convert;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.system.domain.GiftProbability;
import com.qinggeng.project.system.mapper.GiftProbabilityMapper;
import com.qinggeng.project.system.service.IGiftProbabilityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;

@Service
public class GiftProbabilityServiceImpl implements IGiftProbabilityService {
    @Resource
    private GiftProbabilityMapper probabilityMapper;
    @Override
    public int insertGiftProbability(GiftProbability giftProbability) {
        return probabilityMapper.insertGiftProbability(giftProbability);
    }

    @Override
    public int updateGiftProbability(GiftProbability giftProbability) {
        return probabilityMapper.updateGiftProbability(giftProbability);
    }

    @Override
    public List<GiftProbability> selectGiftProbabilityList(GiftProbability giftProbability) {
        return probabilityMapper.selectGiftProbabilityList(giftProbability);
    }

    @Override
    public GiftProbability selectGiftProbabilityById(Long id) {
        return probabilityMapper.selectGiftProbabilityById(id);
    }

    @Override
    public int deleteGiftProbabilityByIds(Long[] ids) {
        return probabilityMapper.deleteGiftProbabilityByIds(ids);
    }

    /**
     * 根据商品价格查询助力次数
     * @param goodsPrice
     */
    @Override
    public GiftProbability selectHelpNumByGoodsPrice(BigDecimal goodsPrice) {
        return probabilityMapper.selectHelpNumByGoodsPrice(goodsPrice);
    }

    @Override
    public BigDecimal getGiftProbability(String giftRate) {
        if(StringUtils.isEmpty(giftRate)){
            throw new CustomException("中奖率不能为空",ShowTypeEnum.用户.getValue());
        }
       BigDecimal probability= Convert.strToBigDecimal(giftRate,new BigDecimal(0));
        if(probability.compareTo(new BigDecimal(0))<0
                ||probability.compareTo(new BigDecimal(1))>0){
            throw new CustomException("中奖率必须在0到1之间",ShowTypeEnum.用户.getValue());
        }
        return probability;
    }
}
